package com.cskaoyan.user.controller;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dto.*;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.IRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 长平居士 - rolfcui@outlook.com
 * @description ${什么模块下的什么功能}
 * @date 2022/7/30 22:05
 */
@RestController
public class RegisterController {

    @Autowired
    private IKaptchaService iKaptchaService;

    @Autowired
    private IRegisterService registerService;

    @PostMapping("/user/register")
    public ResponseData register(@RequestBody Map<String, String> map, HttpServletRequest req) {
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");
        String email = map.get("email");

        if (!email.matches("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$")){
            return new ResponseUtil().setErrorMsg(SysRetCodeConstants.EMAIL_FORMAT_ERROR.getMessage());
        }

        // 验证码阶段
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(req, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse response = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        String code = response.getCode();
        if (!code.equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }

        // 向用户表中插入数据
        UserRegisterRequest registerRequest = new UserRegisterRequest();
        registerRequest.setUserName(userName);
        registerRequest.setUserPwd(userPwd);
        registerRequest.setEmail(email);
        UserRegisterResponse registerResponse = registerService.register(registerRequest);
        if (registerResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null);
        }

        return new ResponseUtil<>().setErrorMsg(registerResponse.getMsg());
    }

    @GetMapping("/user/verify")
    public String verify(UserVerifyRequest request) {
        UserVerifyResponse response = registerService.verify(request);
        if (response.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return "<!DOCTYPE html>\n" +
                    "<html lang='zh'>\n" +
                    "<head>\n" +
                    "    <meta charset='UTF-8'>\n" +
                    "    <meta http-equiv='X-UA-Compatible' content='IE=edge'>\n" +
                    "    <meta name='viewport' content='width=device-width, initial-scale=1.0'>\n" +
                    "    <meta http-equiv='refresh' content='3; url=http://localhost:9999/#/login'>\n" +
                    "    <title>验证成功</title>\n" +
                    "</head>\n" +
                    "<body>\n" +
                    "    <p>验证成功</p>\n" +
                    "    <p>3秒后跳转到登录界面</p>\n" +
                    "</body>\n" +
                    "</html>";
        }
        return "验证失败：" + response.getMsg();
    }

}
